<template>
	<view class="content">
		<view class="add-cicle" @click="addShopClick" v-if="goodsList.length>0">
			<img src="https://file.casugn.com/storecompcard//dnjQCUkRUXE760b1c113ff6a4615abf6c68cd2a58582_1681283943428.png"
				alt="">
		</view>
		<view class="navbar">
			<!-- <u-tabs :list="statusMenuLists" lineColor="#276AEE"
				itemStyle="background:#ffffff;height:80rpx;font-size: 28rpx;font-weight: 600;color: #1D2129;"
				:activeStyle="{
			    color: '#276AEE'
			}" @click="tabClick"></u-tabs> -->

			<view class="nav-item" :class="{current: filterIndex === 0}" @click="tabClick(0)">
				直营
			</view>
			<view class="nav-item" :class="{current: filterIndex === 1}" @click="tabClick(1)">
				代言
			</view>



		</view>
		<view class="goods-list">
			<view v-for="(item, index) in goodsList" :key="index" class="goods-item" @click="navToDetailPage(item)">
				<view class="image-wrapper">
					<image
						:src="item.photo==null?'https://file.casugn.com/storecompcard//4T4oGCfkpPX5a59439bdbafef1bd4ed417e476813128_1660543910299.png':item.photo"
						mode="aspectFill"></image>
				</view>
				<text class="title clamp">{{item.name}}</text>
				<view class="price-box">
					<view class="add-to-car" @tap.stop="deleteClick(item)" v-if="addShop">
						解绑
					</view>
				</view>
			</view>
			<view class="list-null-box" v-if="goodsList.length==0">
				<img src="https://file.casugn.com/storecompcard/temp/空页面@2x(11)_1681113828976.png" alt="">
				<view class="list-null-text">
					{{ filterIndex ?'暂无代言店铺':'暂无直营店铺'}}
				</view>
				<view class="list-null-add" @click="addShopClick">
					添加店铺
				</view>
			</view>
		</view>
		<uni-load-more :status="loadingType"></uni-load-more>
		<!-- 添加直營店鋪 -->
		<u-popup :show="addShow1" mode="center" :round="10" @open="open" @close="close" class="add-pop">
			<view class="add-show-box">

				<u--form>
					<view class="add-show-title">
						请输入直营店铺账号密码
					</view>
					<view class="add-item">
						<u--input v-model="userName" placeholder="账号"></u--input>
					</view>
					<view class="add-item">
						<u--input v-model="password" placeholder="密码" type="password"></u--input>
					</view>
					<view class="add-item add-item-submit">
						<u-button color="#3e2eff" text="确认添加" @click="addConfirm" type="primary">
						</u-button>
					</view>
					<view class="add-item">
						<view>如未注册店铺</view>
						<view>请点击下方按钮复制链接去PC端创建店铺</view>
					</view>
					<view class="add-item">
						<!-- @click="copy(111)" -->
						<u-button color="#276AEE" :plain="true" :hairline="true" @click="copy" text="注册店铺"
							type="primary">
						</u-button>
					</view>


				</u--form>

			</view>
		</u-popup>
		<!-- 是否确认删除 -->
		<u-modal :show="modalShow" :showCancelButton="true" @confirm="modalConfirm" @cancel="modalShow=false"
			@close="modalShow=false" title="是否确认解绑?" :closeOnClickOverlay="true"></u-modal>

	</view>
</template>

<script>
	import api from '@/api/api.js'
	import uniCopy from '@/js_sdk/xb-copy/uni-copy.js'
	import {
		mapState
	} from 'vuex'
	export default {
		components: {},
		data() {
			return {
				statusMenuLists: [{
					name: '直营',
					id: 0,
				}, {
					name: '代言',
					id: 1,
				}],
				cateMaskState: 0, //分类面板展开状态
				headerPosition: "fixed",
				headerTop: "0px",
				loadingType: 'more', //加载更多状态
				filterIndex: 0,
				cateId: 0, //已选三级分类id
				priceOrder: 0, //1 价格从低到高 2价格从高到低
				cateList: [],
				goodsList: [],
				cardId: '',
				yellowCarStatus: '',
				isMyself: false, //判断是否是本人登录，true是本人，false是通过分享进来
				addShop: false, //添加店铺显示  如果不是本人 不显示  如果是本人 判断是否是小黄车进入 小黄车进入1 不显示  不是小黄车 0 显示
				addShow1: false,
				userName: '',
				password: '',
				openId: '',
				modalShow: false,
				deleteId: '',
				pageNo: 1,
				pageSize: 5,
				total: 0

			};
		},

		onLoad(options) {
			console.log(options)
			// #ifdef H5
			// this.headerTop = document.getElementsByTagName('uni-page-head')[0].offsetHeight+'px';
			// #endif
			this.cateId = options.tid;
			// this.yellowCarStatus=options.yellowCarStatus
			// this.loadCateList(options.fid,options.sid);
			// this.loadData();
			// this.getMyStore()

		},
		onPageScroll(e) {
			//兼容iOS端下拉时顶部漂移
			if (e.scrollTop >= 0) {
				this.headerPosition = "fixed";
			} else {
				this.headerPosition = "absolute";
			}
		},
		//下拉刷新
		// onPullDownRefresh(){
		// 	this.loadData('refresh');

		// },
		//加载更多
		// onReachBottom(){
		// 	this.loadData();
		// },
		onShow() {
			let openId = uni.getStorageSync('openId')
			this.openId = openId.openid
			console.log("分享状态", uni.getStorageSync('sceneStatus'));
			let status = uni.getStorageSync('sceneStatus')
			// this.gotoSet=status
			if (status == 1007 || status == 1008 || status == 1044 || status == 1047) {
				this.isMyself = false
			} else {
				this.isMyself = true
			}
			// this.isMyself=this.$store.state.isSelf
			// console.log("是否是本人",a)
			this.yellowCarStatus = uni.getStorageSync('yellowCarStatus')
			console.log("小黄测状态状态" + this.yellowCarStatus)
			if (this.isMyself == false) {
				this.addShop = false
			} else {
				this.yellowCarStatus = uni.getStorageSync('yellowCarStatus')
				console.log("小黄测状态状态" + this.yellowCarStatus)
				if (this.yellowCarStatus == 1) {
					this.addShop = false
				} else {
					this.addShop = true
				}
			}
			// 1是小黄车进入，0是商场进入
			console.log("小黄测进来", this.yellowCarStatus)
			this.getCardId()
			this.goodsList = []
			this.getMyStore()

		},
		methods: {
			// 触发方法
			copy() {
				uniCopy({
					content: 'http://seller.casugn-shop.com/',
					success: (res) => {
						uni.showToast({
							title: res,
							icon: 'none'
						})
					},
					error: (e) => {
						uni.showToast({
							title: e,
							icon: 'none',
							duration: 3000,
						})
					}
				})
			},
			open() {
				// console.log('open');
				this.userName = ""
				this.password = ""
			},
			close() {
				this.addShow1 = false
				// console.log('close');
			},
			addConfirm() {
				if (this.userName == "") {
					uni.showToast({
						title: "账号不能为空",
						icon: 'none'
					});
					return false
				}
				if (this.password == "") {
					uni.showToast({
						title: "密码不能为空",
						icon: 'none'
					});
					return false
				}
				let data = {
					openId: this.openId,
					password: this.password,
					userName: this.userName
				}
				this.$api.addShopToZYList(data).then(res => {
					console.log(res)
					if (res.success) {
						console.log('我的店铺结果', res);

						uni.showToast({
							title: res.message,
							icon: 'none'
						});
						this.userName = '',
							this.password = ''
						this.addShow1 = false
						this.filterIndex = 0
						this.getMyStore()
					} else {
						uni.showToast({
							title: '账号或密码错误',
							icon: 'none'
						});
						this.userName = '',
							this.password = ''
					}
				})

			},



			addShopClick() {
				if (this.filterIndex == 0) {
					this.addShow1 = true
				} else {
					uni.navigateTo({
						url: `/pagesBusiness/shopList/addShop`
					})
				}

			},
			//获取cardId
			getCardId() {
				let cardId = uni.getStorageSync('cardId')
				console.log("这是cardId", cardId)
				if (cardId) {
					// console.log(cardId,'getCardId');
					this.cardId = cardId
				} else {
					console.log('获取cardId失败');
					this.noCreatedCardInfo = '请先创建名片'
				}
			},

			// 获取我的门店
			getMyStore() {
				let openId = uni.getStorageSync('openId')
				console.log("这是openId", openId)
				console.log("这是状态", this.filterIndex)
				this.$api.getMyShop(openId.openid, this.filterIndex).then(res => {
					console.log(res)
					if (res.success) {
						console.log('我的店铺结果', res.result.records);
						this.goodsList = res.result.records
					}
				})



			},

			//加载商品 ，带下拉刷新和上滑加载
			async loadData(type = 'add', loading) {
				//没有更多直接返回
				if (type === 'add') {
					if (this.loadingType === 'nomore') {
						return;
					}
					this.loadingType = 'loading';
				} else {
					this.loadingType = 'more'
				}
				this.getMyStore()
				let goodsList = this.goodsList

				if (type === 'refresh') {
					this.goodsList = [];
				}
				//筛选，测试数据直接前端筛选了
				if (this.filterIndex === 1) {
					goodsList.sort((a, b) => b.sales - a.sales)
				}
				if (this.filterIndex === 2) {
					goodsList.sort((a, b) => {
						if (this.priceOrder == 1) {
							return a.price - b.price;
						}
						return b.price - a.price;
					})
				}

				this.goodsList = this.goodsList.concat(goodsList);

				//判断是否还有下一页，有是more  没有是nomore(测试数据判断大于20就没有了)
				this.loadingType = this.goodsList.length > 20 ? 'nomore' : 'more';
				if (type === 'refresh') {
					if (loading == 1) {
						uni.hideLoading()
					} else {
						uni.stopPullDownRefresh();
					}
				}
			},
			//筛选点击
			tabClick(item) {
				console.log(item)
				let index = 0
				index = item
				if (this.filterIndex === index && index !== 2) {
					return;
				}
				this.filterIndex = index;
				if (index === 2) {
					this.priceOrder = this.priceOrder === 1 ? 2 : 1;
				} else {
					this.priceOrder = 0;
				}
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				})
				this.pageNo = 1
				this.goodsList = []
				this.loadData('refresh', 1);
				uni.showLoading({
					title: '正在加载'
				})
			},
			//显示分类面板
			// toggleCateMask(type){
			// 	let timer = type === 'show' ? 10 : 300;
			// 	let	state = type === 'show' ? 1 : 0;
			// 	this.cateMaskState = 2;
			// 	setTimeout(()=>{
			// 		this.cateMaskState = state;
			// 	}, timer)
			// },
			//分类点击
			changeCate(item) {
				this.cateId = item.id;
				this.toggleCateMask();
				uni.pageScrollTo({
					duration: 300,
					scrollTop: 0
				})
				this.loadData('refresh', 1);
				uni.showLoading({
					title: '正在加载'
				})
			},
			//详情
			navToDetailPage(item) {
				let id = item.storeId;
				uni.navigateTo({
					url: `/pagesBusiness/goodsList/goodsList?storeId=${id}` //修复店铺传参问题,下一页面的接收参数还有goodIds ?
				})
			},

			// 删除店铺点击时间
			deleteClick(item) {
				console.log(item)
				this.modalShow = true
				this.deleteId = item.storeId
			},
			// 确认删除
			modalConfirm() {
				let openId = uni.getStorageSync('openId')
				console.log("这是openId", openId)
				this.$api.cancelMyShop(openId.openid, this.deleteId).then(res => {
					console.log(res)
					if (res.success) {
						this.modalShow = false
						uni.showToast({
							title: res.result,
							icon: 'none'
						})
						// this.pageNo = 1
						this.goodsList = []
						this.getMyStore()
					} else {
						uni.showToast({
							title: res.result,
							icon: 'none'
						})
					}

				})
			},

		},
	}
</script>

<style lang="scss" scoped>
	page,
	.content {
		// background: #F8F9FF;
		position: relative;
	}

	.content {
		padding-top: 96upx;
		position: relative;

		.add-cicle {
			position: fixed;
			width: 112rpx;
			height: 112rpx;
			background: #276AEE;
			box-shadow: 0rpx 16rpx 32rpx 0rpx rgba(22, 63, 169, 0.4);
			text-align: center;
			border-radius: 50%;
			right: 30upx;
			bottom: 188upx;
			z-index: 10;

			img {
				width: 64rpx;
				height: 64rpx;
				position: relative;
				top: 50%;
				transform: translateY(-32rpx);
			}

		}
	}

	.navbar {
		position: fixed;
		left: 0;
		top: 0; //暂定为0 
		display: flex;
		width: 100%;
		height: 80upx;
		background: #fff;
		z-index: 10;

		.nav-item {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 120rpx;
			height: 80upx;
			line-height: 80rpx;
			font-size: 28rpx;
			font-family: PingFang SC-Semibold, PingFang SC;
			font-weight: 600;
			color: #1D2129;
			position: relative;

			&.current {
				color: #423BFE;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 48upx;
					height: 0;
					border-bottom: 4upx solid #423BFE;
				}
			}
		}

		.p-box {
			display: flex;
			flex-direction: column;

			.yticon {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 30upx;
				height: 14upx;
				line-height: 1;
				margin-left: 4upx;
				font-size: 26upx;
				color: #888;

				&.active {
					color: #423BFE;
				}
			}

			.xia {
				transform: scaleY(-1);
			}
		}

		.cate-item {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			width: 80upx;
			position: relative;
			font-size: 26upx;

			&:after {
				content: '';
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				border-left: 1px solid #ddd;
				width: 0;
				height: 36upx;
			}
		}
	}

	/* 分类 */
	.cate-mask {
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 95;
		transition: .3s;

		.cate-content {
			width: 630upx;
			height: 100%;
			background: #fff;
			float: right;
			transform: translateX(100%);
			transition: .3s;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.cate-content {
				transform: translateX(0);
			}
		}
	}

	.cate-list {
		display: flex;
		flex-direction: column;
		height: 100%;

		.cate-item {
			display: flex;
			align-items: center;
			height: 90upx;
			padding-left: 30upx;
			font-size: 28upx;
			color: #555;
			position: relative;
		}

		.two {
			height: 64upx;
			color: #303133;
			font-size: 30upx;
			background: #f8f8f8;
		}

		.active {
			color: #423BFE;
		}
	}

	/* 商品列表 */
	.goods-list {
		display: flex;
		flex-wrap: wrap;
		padding: 32upx;
		background: #fff;


		.list-null-box {
			width: 240upx;
			height: 82vh;
			margin: 0upx auto;
			text-align: center;
			font-size: 26upx;
			margin-top: 240rpx;

			img {
				width: 240rpx;
				height: 240rpx;
			}

			.list-null-text {
				font-size: 28rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #86909C;
				line-height: 39rpx;
				margin-top: 16rpx;
			}

			.list-null-add {
				width: 176rpx;
				height: 64rpx;
				background: #276AEE;
				border-radius: 4rpx 4rpx 4rpx 4rpx;
				color: white;
				line-height: 64rpx;
				margin: 0 auto;
				margin-top: 16rpx;
			}
		}

		.goods-item {
			display: flex;
			flex-direction: column;
			width: 48%;
			margin-bottom: 32upx;
			padding-bottom: 20upx;

			&:nth-child(2n+1) {
				margin-right: 4%;
			}
		}

		.image-wrapper {
			width: 333rpx;
			height: 333rpx;
			overflow: hidden;
			margin-top: 0;
			border-radius: 16rpx !important;

			image {
				width: 100%;
				height: 100%;
			}
		}

		.title {
			font-size: 28rpx;
			font-family: PingFang SC-Semibold, PingFang SC;
			font-weight: 600;
			color: #1D2129;
			line-height: 39rpx;
			margin-top: 24rpx;
		}

		.price-box {
			font-size: 22upx;
			color: #999999;
			margin-top: 16rpx;

			text {
				color: #3e2eff;
				border: 1px solid #3e2eff;
				border-radius: 2upx;
				padding: 0 5upx;
			}

			.add-to-car {
				width: 112rpx;
				height: 48rpx;
				background: #276AEE;
				font-size: 24rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
				line-height: 48rpx;
				text-align: center;
			}
		}

		.price {
			font-size: 32upx;
			color: #DC2626;
			line-height: 1;

			&:before {
				content: '￥';
				font-size: 26upx;
			}
		}
	}


	.add-show-box {
		text-align: center;
		border-radius: 10upx;
		margin: 0 auto;
		padding: 40rpx 32rpx;


		.add-show-title {
			// background: linear-gradient(270deg, #3a3af5 0%, #8888ff 100%);
			height: 120upx;
			line-height: 120upx;
			height: 48rpx;
			font-size: 34rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #1D2129;
			line-height: 48rpx;
			position: relative;

			.add-close {
				position: absolute;
				z-index: 10;
				top: 0upx;
				right: 0upx;
			}
		}

		.add-item {
			margin-top: 25upx;
			width: 558rpx;
			font-size: 27rpx;
			    line-height: 40rpx;
			    color: #6b6a6a;
			// padding: 0 60upx
		}

		.add-item-submit {
			padding-bottom: 30upx
		}

	}
</style>
